package com.bluefish.bltuan.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bluefish.bltuan.dao.UserDao;
import com.bluefish.bltuan.module.user.AbstractUser;
import com.bluefish.bltuan.module.user.User;
import com.bluefish.bltuan.service.UserService;
import com.bluefish.bltuan.util.PageIndex;

@Service
@Transactional
public class UserServiceImpl implements UserService{

	private static final int NO_SUCH_USER = 100;

	private static final int ERROR_PASSWORD = 200;
	
	private static final int NO_ACTIVATED = 300;
	
    private static final int SUCC_VALIDATED = 400;
	
	private UserDao userDao;
	
	public User getAnonymousUser() {
		// TODO Auto-generated method stub
		return null;
	}

	public void delete(User t) {
		this.userDao.delete(t);
	}

	public User findById(Integer id) {
		return this.userDao.findById(id);
	}
	
	public AbstractUser findByUserId(Integer id) {
		return this.userDao.findByUserId(id);
	}

	public void save(User t) {
		this.userDao.saveOrUpdate(t);
	}

	public void update(User t) {
		this.userDao.update(t);
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public User doLogin(String email, String password) {
		return this.userDao.getByEmailAndPasswordl(email, password);
	}

	public int validateUser(String email, String password) {
		int msg = 0;
		User user = this.userDao.findByEmail(email); 
		if(user == null){
			msg = NO_SUCH_USER;
		}else {
			msg = !user.getPassword().equals(password) ? ERROR_PASSWORD : user.isActivated() ? SUCC_VALIDATED : NO_ACTIVATED; 
		}
		
		return msg;
	}

	public List<User> getAll(PageIndex pageIndex) {
		return this.userDao.list(pageIndex);
	}

	public User getByEmail(String email) {
		return this.userDao.findByEmail(email);
	}

	public User getByName(String name) {
		return this.userDao.findByUsername(name);
	}
	
}
